package com.op.util;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.os.Environment;
import android.util.Log;

public class DirUtils {

	private static final String TAG = DirUtils.class.getSimpleName();
	
	private Context mContext;
	private String baseDir;
	private String userDir;
	
	public static final String DIR_PATH_SEPARATOR = "/";
	private static final String[] COURSES_RESOURCE_DETAILS = {"课程ppt", "上机实验", 
		"视频资料", "课程案例", "课程计划", "学生名单"};
	
	public DirUtils(Context context, String userId) {
		this.mContext = context;
		this.baseDir = mContext.getFilesDir().getAbsolutePath();
		this.userDir = baseDir + "/user-" + userId + DIR_PATH_SEPARATOR;
	}
	
	public void initDirs(List<String> courses) {
    	if (ListUtils.isEmpty(courses)) {
			return;
		}
    	
    	for (String course : courses) {
    		String courseDir = userDir + course + DIR_PATH_SEPARATOR;
			FileUtils.makeDirs(courseDir);
			for (int i = 0; i < COURSES_RESOURCE_DETAILS.length; i++) {
				String detailDir = courseDir + COURSES_RESOURCE_DETAILS[i] + DIR_PATH_SEPARATOR;
				FileUtils.makeDirs(detailDir);
				Log.i(TAG, "create dir : " + detailDir);
			}
		}
    }
	
	public void initFiles() {
		String copyFileDir = Environment.getExternalStorageDirectory().getAbsolutePath();
		Log.i(TAG, copyFileDir);
	}
	
	/**
	 * 获取当前目录下的文件名
	 * 
	 * @param currentDir 需获取文件的目录，如"C语言/ppt"
	 * @return
	 */
	public List<String> getFilesNameByDir(String currentDir) {
		String path = userDir + currentDir + DIR_PATH_SEPARATOR;
		return getFilesName(path);
	}
	
	public List<String> getFilesName(String path) {
		if (!FileUtils.isFolderExist(path)) {
			return null;
		}
		
		List<String> fileList = new ArrayList<String>();
		File[] files = new File(path).listFiles();
		for (File file : files) {
			if (file.isFile()) {
				fileList.add(file.getName());
			}
			Log.i(TAG, file.getName());
		}
		
		return fileList;
	}
}
